package com.edu.udistrital.indexing.analyzer;

import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.LowerCaseFilter;
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.es.SpanishLightStemFilter;
import org.apache.lucene.analysis.snowball.SnowballFilter;
import org.apache.lucene.analysis.standard.StandardFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.synonym.SynonymFilter;
import org.apache.lucene.util.Version;
import org.tartarus.snowball.ext.SpanishStemmer;

import com.edu.udistrital.indexing.analyzer.eswordnet.SynonymEsWordNet;
import com.edu.udistrital.util.Configuration;

public class AnalyzerClassification extends Analyzer{

	private SynonymEsWordNet synonymEsWordNet;
	
	/**
	 * 
	 */
	public AnalyzerClassification(Version version) {
		super(version, Configuration.getInstance().getProperty("stopWordsSpanish"));
	}

	@Override
	@SuppressWarnings("deprecation")
	protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
	    final Tokenizer source = new StandardTokenizer(MATCH_VERSION, reader);
	    TokenStream result = new StandardFilter(MATCH_VERSION, source);
	    result = new LowerCaseFilter(MATCH_VERSION, result);
	    result = new StopFilter(MATCH_VERSION, result, STOPWORDS);
	    
	    try {
	    	synonymEsWordNet = new SynonymEsWordNet();
	    	if(synonymEsWordNet.getSynMapEsWordNet().maxHorizontalContext > 0) {
	    		result = new SynonymFilter(result, synonymEsWordNet.getSynMapEsWordNet(), true);
	    	}
		} catch (IOException e) {
			e.printStackTrace();
		}

	    if (MATCH_VERSION.onOrAfter(Version.LUCENE_36)) {
	      result = new SpanishLightStemFilter(result);
	    } else {
	      result = new SnowballFilter(result, new SpanishStemmer());
	    }
	    return new TokenStreamComponents(source, result);
	}

}
